<?php
class ControllerArticleCategory extends FrontController {
	public function index() {
		$this->document->setTitle($this->_['heading_title']);
		$this->data['title'] = $this->_['heading_title'];
		
		$category_id = intval(G('category_id', 0));
		
		M('article/article');			
		
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->_['heading_title'],
				'href'      => U('article/category'),
				'separator' => $this->_['text_separator']
		);
		
		if ($category_id) {
			$category = $this->model_article_article->getCategory($category_id);
			
			if ($category) {
				$this->data['breadcrumbs'][] = array(
						'text'      => $category['name'],
						'href'      => U('article/category', 'category_id=' . $category_id),
						'separator' => $this->_['text_separator']
				);
				$this->data['title'] = $category['name'];
				if ($category['seo_title']) {
					$this->document->setTitle($category['seo_title'] . ' - ' . $this->_['heading_title']);
				}
				else {
					$this->document->setTitle($category['name'] . ' - ' . $this->_['heading_title']);
				}
				$this->document->setKeywords($category['meta_keywords']);
				$this->document->setDescription($category['meta_description']);
			}
		}		
		$this->document->addStyle('themes/' . C('config_template') . '/stylesheet/article.css');
		
		$this->load->helper('query_filter');
		$qf = new Query_filter(null, false);
		$total = $this->model_article_article->getTotalArticles($qf);
		$this->data['articles'] = $this->model_article_article->getArticles($qf);
		
		foreach ($this->data['articles'] as $k => $v) {
			$this->data['articles'][$k]['link'] = U('article/index', 'article_id=' . $v['article_id']);
			$this->data['articles'][$k]['summary'] = v5_html_entity_decode($v['summary']);
		}
		
		$page = $qf->get('page', 1);
		$url = U('article/category', ($category_id ? "category_id=$category_id" : '') . '&page={page}');
		$this->data['pagination'] = $this->pagination($total, $page, $url)->render();
		
		$this->display("article/category.tpl");
	}
}